package limeirafol.gui.Cadastro;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import org.openide.util.Exceptions;
import limeirafol.gui.MenuGUI;
import limeirafol.remote.cadastro.AcessorioVO;
import limeirafol.bean.BeanCadastro;
import limeirafol.remote.cadastro.Situacao;
import limeirafol.remote.cadastro.TipoAcesorio;
import limeirafol.remote.cadastro.tipoEmbalagem;
import lbframework.bean.internalFrame.LBInternalFrame;
import lbframework.bean.mdiFrame.LBMdiFrame;
import lbframework.classe.Util;
import limeirafol.Global;

public class AcessorioCadastroGUI extends LBInternalFrame {

    public AcessorioVO oAcessorio = new AcessorioVO();
    public BeanCadastro beanCadastro = new BeanCadastro();
    String a = "";

    public AcessorioCadastroGUI(LBMdiFrame i_mdiFrame) throws Exception {
        super(i_mdiFrame);

        initComponents();

        centralizarForm();
        txtCodigo.setEnabled(false);
        cnsFornecedor.setTabela("fornecedor");
        cnsFornecedor.setCampoDescricao("razaosocial");

        cboSituacao.setTabela("situacaocadastro");
        cboSituacao.carregar();

        cboTipoAcessorio.setTabela("tipoacessorio");
        cboTipoAcessorio.carregar();

        cboTipoEmbalagem.setTabela("tipoembalagem");
        cboTipoEmbalagem.carregar();
        cboTipoEmbalagem.setId(2);

        lblFoto.setIcon(new ImageIcon("/LimeiraFol/Imagens/Acessorios/sem_foto.jpg"));
        txtFoto.setText("SEM_FOTO.JPG");
        btnZoom.setVisible(false);

        if (cboSituacao.getId() == Situacao.ATIVO.getId()) {
            cboSituacao.setEnabled(false);
        }

        txtCodigoInterno.requestFocusLater();

    }

    @Override
    public void salvar() throws Exception {
        Util.validarCampoTela(this.getCampoObrigatorio());

        oAcessorio.codigoInterno = txtCodigoInterno.getText();
        oAcessorio.descricao = txtDescricao.getText();
        oAcessorio.codigoExterno = txtCodExterno.getText();
        oAcessorio.id_fornecedor = cnsFornecedor.getId();

        if (!(oAcessorio.id_tipoEmbalagem == tipoEmbalagem.KG.getId())) {
            oAcessorio.estoqueMinimo = txtEstoqueMinimo.getDouble();
            oAcessorio.estoqueMaximo = txtEstoqueMaximo.getDouble();
        } else {
            oAcessorio.estoqueMinimo = txtEstoqueMinimo.getDouble();
            oAcessorio.estoqueMaximo = txtEstoqueMaximo.getDouble();
        }

        oAcessorio.id_situacaocadatro = cboSituacao.getId();
        oAcessorio.pesoUnitario = txtPesoUnitario.getDouble();
        oAcessorio.custoUnitario = txtCustoUnitario.getDouble();
        oAcessorio.foto = txtFoto.getText();
        oAcessorio.id_tipoacessorio = cboTipoAcessorio.getId();
        oAcessorio.id_tipoEmbalagem = cboTipoEmbalagem.getId();

        if (cboSituacao.getId() == Situacao.ATIVO.getId()) {
            cboSituacao.setEnabled(false);
        }

        beanCadastro.salvarAcessorio(oAcessorio);

        if (!txtCodigo.getText().isEmpty()) {
            Global.atualizarCustoAcessorio(oAcessorio.id, txtCustoUnitario.getDouble());
        }

        Util.exibirMensagem(Util.MSG_SALVO_SUCESSO, getTitle());

        getEstadoTelaInicial();

        incluir();
    }

    public void carregarAcessorio(int i_id) throws Exception {
        oAcessorio = beanCadastro.carregarAcessorio(i_id);

        if (!(oAcessorio.id_tipoEmbalagem == tipoEmbalagem.KG.getId())) {
            txtCustoUnitario.setMascara("Decimal 3");
            txtEstoqueMinimo.setMascara("Decimal 0");
            txtEstoqueMaximo.setMascara("Decimal 0");

        } else {
            txtCustoUnitario.setMascara("Decimal 2");
            txtEstoqueMinimo.setMascara("Decimal 3");
            txtEstoqueMaximo.setMascara("Decimal 3");
        }

        txtCodigo.setText(Util.formatNumber(oAcessorio.id, 6));
        txtCodigoInterno.setText(oAcessorio.codigoInterno);
        txtDescricao.setText(oAcessorio.descricao);
        txtCodExterno.setText(oAcessorio.codigoExterno);
        cnsFornecedor.setId(oAcessorio.id_fornecedor);
        cnsFornecedor.consultar();
        txtEstoqueMinimo.setDouble(oAcessorio.estoqueMinimo);
        txtEstoqueMaximo.setDouble(oAcessorio.estoqueMaximo);
        txtPesoUnitario.setDouble(oAcessorio.pesoUnitario);
        txtCustoUnitario.setDouble(oAcessorio.custoUnitario);
        cboSituacao.setId(oAcessorio.id_situacaocadatro);
        txtFoto.setText(oAcessorio.foto);
        cboTipoAcessorio.setId(oAcessorio.id_tipoacessorio);
        cboTipoEmbalagem.setId(oAcessorio.id_tipoEmbalagem);

        if (!(txtFoto.getText().equals("SEM_FOTO.JPG"))) {
            lblFoto.setIcon(new ImageIcon("/LimeiraFol/Imagens/Acessorios/" + oAcessorio.foto));
            btnZoom.setVisible(true);
        } else {
            lblFoto.setIcon(new ImageIcon("/LimeiraFol/Imagens/Acessorios/sem_foto.jpg"));
            txtFoto.setText("sem_foto.jpg");
            btnZoom.setVisible(false);
        }


        if (cboSituacao.getId() == Situacao.EXCLUIDO.getId()) {
            cboSituacao.setEnabled(true);
        }

        txtPesoUnitario.setEnabled(false);

        getEstadoTelaInicial();

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        vRToolBarPadrao1 = new lbframework.bean.toolBarPadrao.LBToolBarPadrao();
        vRPanel1 = new lbframework.bean.panel.LBPanel();
        vRLabel1 = new lbframework.bean.label.LBLabel();
        txtCodigo = new lbframework.bean.textField.LBTextField();
        vRLabel2 = new lbframework.bean.label.LBLabel();
        txtDescricao = new lbframework.bean.textField.LBTextField();
        vRLabel3 = new lbframework.bean.label.LBLabel();
        cnsFornecedor = new lbframework.bean.consultaCampo.LBConsultaCampo();
        vRLabel4 = new lbframework.bean.label.LBLabel();
        txtCodExterno = new lbframework.bean.textField.LBTextField();
        txtCodigoInterno = new lbframework.bean.textField.LBTextField();
        vRLabel5 = new lbframework.bean.label.LBLabel();
        txtEstoqueMinimo = new lbframework.bean.textField.LBTextField();
        vRLabel6 = new lbframework.bean.label.LBLabel();
        vRLabel7 = new lbframework.bean.label.LBLabel();
        txtEstoqueMaximo = new lbframework.bean.textField.LBTextField();
        lblPeso = new lbframework.bean.label.LBLabel();
        txtPesoUnitario = new lbframework.bean.textField.LBTextField();
        lblPreco = new lbframework.bean.label.LBLabel();
        txtCustoUnitario = new lbframework.bean.textField.LBTextField();
        cboSituacao = new lbframework.bean.comboBox.LBComboBox();
        vRLabel10 = new lbframework.bean.label.LBLabel();
        vRLabel11 = new lbframework.bean.label.LBLabel();
        txtFoto = new lbframework.bean.textField.LBTextField();
        vRButton1 = new lbframework.bean.button.LBButton();
        vRLabel12 = new lbframework.bean.label.LBLabel();
        cboTipoAcessorio = new lbframework.bean.comboBox.LBComboBox();
        btnZoom = new lbframework.bean.button.LBButton();
        lblFoto =  new javax.swing.JLabel(new ImageIcon())
        {
            @Override
            public void paintComponent(java.awt.Graphics g) {
                super.paintComponent(g);
                g.drawImage(((ImageIcon)getIcon()).getImage(), 0, 0, getWidth(),                                                       getHeight(), null);
            }
        };
        vRLabel13 = new lbframework.bean.label.LBLabel();
        cboTipoEmbalagem = new lbframework.bean.comboBox.LBComboBox();
        vRPanel2 = new lbframework.bean.panel.LBPanel();
        btnSair = new lbframework.bean.button.LBButton();
        btnSalvar = new lbframework.bean.button.LBButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Acessório Cadastro");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/lbframework/img/anel.png"))); // NOI18N
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosing(evt);
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        vRToolBarPadrao1.setRollover(true);
        vRToolBarPadrao1.setVisibleSalvar(true);

        vRLabel1.setText("Código");

        vRLabel2.setText("Descrição");

        txtDescricao.setColumns(40);
        txtDescricao.setObrigatorio(true);

        vRLabel3.setText("Fornecedor");

        cnsFornecedor.setObrigatorio(true);
        cnsFornecedor.addEventoConsultaCampoListener(new lbframework.bean.consultaCampo.LBEventoConsultaCampoListener() {
            public void abreTelaConsulta(lbframework.bean.consultaCampo.LBEventoConsultaCampo evt) {
                cnsFornecedorAbreTelaConsulta(evt);
            }
            public void aposConsulta(lbframework.bean.consultaCampo.LBEventoConsultaCampo evt) {
                cnsFornecedorAposConsulta(evt);
            }
        });

        vRLabel4.setText("Cód. Externo");

        txtCodExterno.setColumns(10);

        txtCodigoInterno.setColumns(10);
        txtCodigoInterno.setObrigatorio(true);

        vRLabel5.setText("Cód. Interno");

        txtEstoqueMinimo.setMascara("Decimal 0");
        txtEstoqueMinimo.setObrigatorio(true);
        txtEstoqueMinimo.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtEstoqueMinimoFocusLost(evt);
            }
        });

        vRLabel6.setText("Estoque Mínimo");

        vRLabel7.setText("Estoque Máximo");

        txtEstoqueMaximo.setMascara("Decimal 0");

        lblPeso.setText("Peso Unitário (g)");

        txtPesoUnitario.setMascara("Decimal 1");
        txtPesoUnitario.setObrigatorio(true);

        lblPreco.setText("Preço de Custo (Un.)");

        txtCustoUnitario.setMascara("Decimal 3");
        txtCustoUnitario.setObrigatorio(true);

        vRLabel10.setText("Situação");

        vRLabel11.setText("Foto");

        txtFoto.setColumns(100);
        txtFoto.setEnabled(false);

        vRButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lbframework/bean/fileChooser/abrir.png"))); // NOI18N
        vRButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                vRButton1ActionPerformed(evt);
            }
        });

        vRLabel12.setText("Tipo Acessório");

        cboTipoAcessorio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboTipoAcessorioActionPerformed(evt);
            }
        });

        btnZoom.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lbframework/img/zoomin.png"))); // NOI18N
        btnZoom.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnZoomActionPerformed(evt);
            }
        });

        lblFoto.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        vRLabel13.setText("Tipo Embalagem");

        cboTipoEmbalagem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboTipoEmbalagemActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout vRPanel1Layout = new javax.swing.GroupLayout(vRPanel1);
        vRPanel1.setLayout(vRPanel1Layout);
        vRPanel1Layout.setHorizontalGroup(
            vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(vRPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(vRPanel1Layout.createSequentialGroup()
                        .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(vRPanel1Layout.createSequentialGroup()
                                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(vRLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(vRLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtCodigoInterno, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(vRLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(vRLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(cboSituacao, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(vRPanel1Layout.createSequentialGroup()
                                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(vRLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtCodExterno, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(vRLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(cnsFornecedor, javax.swing.GroupLayout.PREFERRED_SIZE, 471, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(vRPanel1Layout.createSequentialGroup()
                                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(cboTipoAcessorio, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(vRLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(cboTipoEmbalagem, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(vRLabel13, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(vRLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtEstoqueMinimo, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtEstoqueMaximo, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(vRLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(vRPanel1Layout.createSequentialGroup()
                                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lblPeso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtPesoUnitario, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lblPreco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtCustoUnitario, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(10, 10, 10)
                        .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnZoom, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblFoto, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(vRPanel1Layout.createSequentialGroup()
                        .addComponent(txtFoto, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(vRButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(vRLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(12, Short.MAX_VALUE))
        );
        vRPanel1Layout.setVerticalGroup(
            vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(vRPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(vRPanel1Layout.createSequentialGroup()
                        .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(vRPanel1Layout.createSequentialGroup()
                                .addComponent(vRLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cboSituacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(vRPanel1Layout.createSequentialGroup()
                                        .addComponent(vRLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(vRPanel1Layout.createSequentialGroup()
                                        .addComponent(vRLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txtCodigoInterno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGroup(vRPanel1Layout.createSequentialGroup()
                                    .addComponent(vRLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(txtDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(vRPanel1Layout.createSequentialGroup()
                                .addComponent(vRLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtCodExterno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(vRPanel1Layout.createSequentialGroup()
                                .addComponent(vRLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cnsFornecedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(vRPanel1Layout.createSequentialGroup()
                                        .addComponent(vRLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                            .addComponent(cboTipoAcessorio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(cboTipoEmbalagem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addGroup(vRPanel1Layout.createSequentialGroup()
                                        .addComponent(vRLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(26, 26, 26)))
                                .addGroup(vRPanel1Layout.createSequentialGroup()
                                    .addComponent(vRLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(txtEstoqueMinimo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(vRPanel1Layout.createSequentialGroup()
                                .addComponent(vRLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtEstoqueMaximo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(vRPanel1Layout.createSequentialGroup()
                                .addComponent(lblPreco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtCustoUnitario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(vRPanel1Layout.createSequentialGroup()
                                .addComponent(lblPeso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtPesoUnitario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(vRPanel1Layout.createSequentialGroup()
                        .addComponent(lblFoto, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnZoom, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(vRLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(vRButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtFoto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lbframework/img/sair.png"))); // NOI18N
        btnSair.setText("Sair");
        btnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairActionPerformed(evt);
            }
        });

        btnSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lbframework/img/salvar.png"))); // NOI18N
        btnSalvar.setText("Salvar");
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout vRPanel2Layout = new javax.swing.GroupLayout(vRPanel2);
        vRPanel2.setLayout(vRPanel2Layout);
        vRPanel2Layout.setHorizontalGroup(
            vRPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, vRPanel2Layout.createSequentialGroup()
                .addContainerGap(506, Short.MAX_VALUE)
                .addComponent(btnSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnSair, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        vRPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnSair, btnSalvar});

        vRPanel2Layout.setVerticalGroup(
            vRPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(vRPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(btnSair, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(btnSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(vRPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(vRPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(vRToolBarPadrao1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(vRToolBarPadrao1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(vRPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(vRPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(19, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtEstoqueMinimoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtEstoqueMinimoFocusLost
        double a1 = 0;
        try {

            a1 = txtEstoqueMinimo.getDouble();
            txtEstoqueMaximo.setDouble(a1 + 40);

        } catch (Exception ex) {
            Exceptions.printStackTrace(ex);
        }

    }//GEN-LAST:event_txtEstoqueMinimoFocusLost

    private void cnsFornecedorAbreTelaConsulta(lbframework.bean.consultaCampo.LBEventoConsultaCampo evt) {//GEN-FIRST:event_cnsFornecedorAbreTelaConsulta
        try {
            MenuGUI menu = (MenuGUI) mdiFrame;

            if (menu.formFornecedorConsulta == null || menu.formFornecedorConsulta.isClosed()) {
                menu.formFornecedorConsulta = new FornecedorConsultaGUI(mdiFrame);
            }

            menu.formFornecedorConsulta.setConsultaCampo(cnsFornecedor);
            menu.formFornecedorConsulta.setVisible(true);

        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, getTitle());
        }
    }//GEN-LAST:event_cnsFornecedorAbreTelaConsulta

    private void cnsFornecedorAposConsulta(lbframework.bean.consultaCampo.LBEventoConsultaCampo evt) {//GEN-FIRST:event_cnsFornecedorAposConsulta

        cboTipoAcessorio.requestFocus();

    }//GEN-LAST:event_cnsFornecedorAposConsulta

    private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSairActionPerformed
        try {
            verificarAlteracaoTela();
            sair();

        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, getTitle());
        }
    }//GEN-LAST:event_btnSairActionPerformed

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed
        try {
            salvar();

        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, getTitle());
        }
    }//GEN-LAST:event_btnSalvarActionPerformed

    private void vRButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_vRButton1ActionPerformed
        //****** Metodo para Selecionar a Foto com JFILE CHOOSER
        //****** Depende do método CopiaFoto ******

        try {
            JFileChooser busca_foto = new JFileChooser();
            busca_foto.setDialogTitle("Selecione a Imagem");

            //****************** FILTRO PRA BUSCAR SOMENTE JPG
            busca_foto.setFileFilter(new javax.swing.filechooser.FileFilter() {

                public boolean accept(File f) {
                    return (f.getName().endsWith(".jpg")) || f.isDirectory();
                }

                public String getDescription() {
                    return "*.JPG";
                }
            });
            busca_foto.setDialogTitle("Carregar imagem do acessorio");
            busca_foto.showOpenDialog(this);
            String foto = "" + busca_foto.getSelectedFile().getName();
            txtFoto.setText(foto);
            File arquivo = busca_foto.getSelectedFile();
            a = arquivo.getCanonicalPath();

            //******************** Copia a Imagem para o Diretorio do Projeto ************
            File arquivo1 = new File(a);
            File arquivo2 = new File("/LimeiraFol/Imagens/Acessorios/" + txtFoto.getText());
            copiarFoto(arquivo1, arquivo2);
            lblFoto.setIcon(new ImageIcon("/LimeiraFol/Imagens/Acessorios/" + txtFoto.getText()));

        } catch (Exception e) {
            //JOptionPane.showMessageDialog(null, "Não foi possivel carregar a foto");
        }
    }//GEN-LAST:event_vRButton1ActionPerformed

    private void cboTipoAcessorioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboTipoAcessorioActionPerformed

        if (cboTipoAcessorio.getId() == TipoAcesorio.MAO_DE_OBRA.getId()) {
            txtEstoqueMaximo.setEnabled(false);
            txtEstoqueMaximo.setObrigatorio(false);
            txtEstoqueMinimo.setEnabled(false);
            txtEstoqueMinimo.setObrigatorio(false);
            cboTipoEmbalagem.setEnabled(false);
            cboTipoEmbalagem.setId(3);
        } else {
            txtEstoqueMaximo.setEnabled(true);
            txtEstoqueMinimo.setEnabled(true);
            txtEstoqueMinimo.setObrigatorio(true);
            cboTipoEmbalagem.setEnabled(true);
            cboTipoEmbalagem.setId(2);
        }

    }//GEN-LAST:event_cboTipoAcessorioActionPerformed

    private void btnZoomActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnZoomActionPerformed
        try {
            AcessorioCadastroFotoGUI form = new AcessorioCadastroFotoGUI(mdiFrame, this);
            form.setVisible(true);

        } catch (Exception ex) {
            Exceptions.printStackTrace(ex);
        }
    }//GEN-LAST:event_btnZoomActionPerformed

    private void cboTipoEmbalagemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboTipoEmbalagemActionPerformed
        if (!(cboTipoEmbalagem.getId() == tipoEmbalagem.KG.getId())) {
            lblPreco.setText("Preço de Custo (Un.)");
            txtPesoUnitario.setEnabled(true);
            txtPesoUnitario.setObrigatorio(true);
            txtCustoUnitario.setMascara("Decimal 3");
            txtEstoqueMinimo.setMascara("Decimal 0");
            txtEstoqueMaximo.setMascara("Decimal 0");

        } else {
            lblPreco.setText("Preço Por Kg");
            txtPesoUnitario.setEnabled(false);
            txtPesoUnitario.setObrigatorio(false);
            txtCustoUnitario.setMascara("Decimal 2");
            txtEstoqueMinimo.setMascara("Decimal 1");
            txtEstoqueMaximo.setMascara("Decimal 1");
        }
    }//GEN-LAST:event_cboTipoEmbalagemActionPerformed

    private void formInternalFrameClosing(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosing
        try {
            verificarAlteracaoTela();
            sair();

        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, getTitle());
        }
    }//GEN-LAST:event_formInternalFrameClosing

    public void copiarFoto(File src, File dst) throws IOException {
        InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dst);           // Transferindo bytes de entrada para saída
        byte[] buf = new byte[10000];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private lbframework.bean.button.LBButton btnSair;
    private lbframework.bean.button.LBButton btnSalvar;
    private lbframework.bean.button.LBButton btnZoom;
    private lbframework.bean.comboBox.LBComboBox cboSituacao;
    private lbframework.bean.comboBox.LBComboBox cboTipoAcessorio;
    private lbframework.bean.comboBox.LBComboBox cboTipoEmbalagem;
    private lbframework.bean.consultaCampo.LBConsultaCampo cnsFornecedor;
    private javax.swing.JLabel lblFoto;
    private lbframework.bean.label.LBLabel lblPeso;
    private lbframework.bean.label.LBLabel lblPreco;
    private lbframework.bean.textField.LBTextField txtCodExterno;
    private lbframework.bean.textField.LBTextField txtCodigo;
    private lbframework.bean.textField.LBTextField txtCodigoInterno;
    private lbframework.bean.textField.LBTextField txtCustoUnitario;
    private lbframework.bean.textField.LBTextField txtDescricao;
    private lbframework.bean.textField.LBTextField txtEstoqueMaximo;
    private lbframework.bean.textField.LBTextField txtEstoqueMinimo;
    private lbframework.bean.textField.LBTextField txtFoto;
    private lbframework.bean.textField.LBTextField txtPesoUnitario;
    private lbframework.bean.button.LBButton vRButton1;
    private lbframework.bean.label.LBLabel vRLabel1;
    private lbframework.bean.label.LBLabel vRLabel10;
    private lbframework.bean.label.LBLabel vRLabel11;
    private lbframework.bean.label.LBLabel vRLabel12;
    private lbframework.bean.label.LBLabel vRLabel13;
    private lbframework.bean.label.LBLabel vRLabel2;
    private lbframework.bean.label.LBLabel vRLabel3;
    private lbframework.bean.label.LBLabel vRLabel4;
    private lbframework.bean.label.LBLabel vRLabel5;
    private lbframework.bean.label.LBLabel vRLabel6;
    private lbframework.bean.label.LBLabel vRLabel7;
    private lbframework.bean.panel.LBPanel vRPanel1;
    private lbframework.bean.panel.LBPanel vRPanel2;
    private lbframework.bean.toolBarPadrao.LBToolBarPadrao vRToolBarPadrao1;
    // End of variables declaration//GEN-END:variables
}
